package pers.qianyu.month_202012.date_20201213;

/**
 * 303. 区域和检索 - 数组不可变
 * https://leetcode-cn.com/problems/range-sum-query-immutable/
 *
 * @author mizzle rain
 * @date 2020-12-13 20:38
 */
public class NumArray {
    // sums[i] 表示 0 ~ i 所有数字的和
    private int[] sums;

    public NumArray(int[] nums) {
        sums = new int[nums.length];
        if (sums.length > 0) {
            sums[0] = nums[0];
            for (int i = 1; i < nums.length; i++) {
                sums[i] = sums[i - 1] + nums[i];
            }
        }
    }

    public int sumRange(int i, int j) {
        if (i == 0) {
            return sums[j];
        }
        return sums[j] - sums[i - 1];
    }
}
